﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Note_CHUSHI
{
    public partial class LoginForm : Form
    {        
        public LoginForm()
        {
            InitializeComponent(); 
            SetupUI();         
        }
           
        private void SetupUI()
        {
            this.Text = "笔记系统登录";
            this.Size = new Size(1000, 700);
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;

            var mainPanel = new Panel { Dock = DockStyle.Fill };

            Font labelFont = new Font("微软雅黑", 12, FontStyle.Bold);
            Font textBoxFont = new Font("微软雅黑", 12);
            Font buttonFont = new Font("微软雅黑", 12, FontStyle.Bold);


            //居中布局
            var centerPanel = new Panel
            {
                Width = 400,
                Height = 300,
                Location = new Point((this.Width - 400) / 2, (this.Height - 300) / 2)
            };

            //用户名输入
            var lblUser = new Label
            {
                Text = "用户名",
                Font = labelFont,
                Top = 50,
                Left = 50,
                Width = 80
            };

            var txtUser = new TextBox
            {
                Font = textBoxFont,
                Top = 50,
                Left = 130,
                Width = 200,
                Height = 30  
            };

            //密码输入
            var lblPass = new Label
            {
                Text = "密码",
                Font = labelFont,
                Top = 100,
                Left = 50,
                Width = 80
            };

            var txtPass = new TextBox
            {
                Font = textBoxFont,
                Top=100,
                Left = 130,
                Width=200,
                Height=30,
                PasswordChar= '*'
            };

            //按钮
            var btnLogin = new Button
            {
                Text = "登录",
                Font = buttonFont,
                Top = 160,
                Left = 100,
                Width = 100,
                Height = 40
            };

            var btnRegister = new Button
            {
                Text = "注册",
                Font = buttonFont,
                Top = 160,
                Left = 220,
                Width = 100,
                Height = 40
            };

            // 状态标签
            var lblStatus = new Label
            {
                Text = "",
                Font = textBoxFont,
                Top = 220,
                Left = 50,
                Width = 300,
                ForeColor = Color.Red
            };



            btnLogin.Click += (sender, e) =>
            {
                if (AccountService.Login(txtUser.Text, txtPass.Text))
                {
                    lblStatus.Text = "登录成功！";
                   
                    this.Hide();
                    var notesForm = new NotesListForm(txtUser.Text);
                    notesForm.FormClosed += (s, args) =>
                    {
                        this.Show();
                        txtPass.Text = "";
                    };
                    notesForm.Show();
                    
                }
                else
                {
                    lblStatus.Text = "用户名或密码错误！";
                }
            };

            btnRegister.Click += (sender, e) =>
            {
                if (AccountService.Register(txtUser.Text, txtPass.Text))
                {
                    lblStatus.Text = "注册成功，请登录！";
                }
                else
                {
                    lblStatus.Text = "注册失败（用户名不合法或已存在）";
                }
            };

            // 添加控件到居中面板
            centerPanel.Controls.AddRange(new Control[] {
                lblUser, txtUser,
                lblPass, txtPass,
                btnLogin, btnRegister,
                lblStatus
            });

            // 添加居中面板到主面板
            mainPanel.Controls.Add(centerPanel);
            this.Controls.Add(mainPanel);

        }            
    }
}
